﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Stock.Common
{
    internal class CaclCommon
    {
        #region 取得利息

        /// <summary>
        /// 取得利息
        /// </summary>
        /// <param name="baseMoney">基数</param>
        /// <param name="starDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="decRate">利率</param>
        /// <returns>利息</returns>
        public static Decimal getInterest(Decimal baseMoney, DateTime starDate, DateTime endDate, Decimal decRate)
        {
            Decimal back = 0m;
            Decimal backYear = 0m;
            try
            {
                int iYear = 0;

                int invDays = (endDate - starDate).Days;

                iYear = invDays / 365;

                endDate = endDate.AddYears(-iYear);
                invDays = (endDate - starDate).Days;

                if (invDays > 360)
                {
                    invDays = 360;
                }

                for (int i = 0; i < iYear; i++)
                {
                    Decimal decInterest = baseMoney * decRate;

                    baseMoney += decInterest;
                    backYear += decInterest;
                }

                back = baseMoney * invDays / 360 * decRate;
                back += backYear;
                back = Math.Round(back, 2);
            }
            catch { }

            return back;
        }

        #endregion 取得利息

        #region 取得每次操作后的累计值

        /// <summary>
        /// 取得每次操作后的累计值
        /// </summary>
        /// <param name="baseList">操作金额列表</param>
        /// <returns>每次操作后的累计值</returns>
        public static List<Decimal> getSumList(List<Decimal> baseList)
        {
            List<Decimal> backList = new List<Decimal>();

            int listCount = 0;

            foreach (Decimal data in baseList)
            {
                if (listCount == 0)
                {
                    backList.Add(data);
                }
                else
                {
                    Decimal temp = backList[listCount - 1] + data;
                    backList.Add(temp);
                }
                listCount++;
            }
            //backList.Sort();

            return backList;
        }

        #endregion 取得每次操作后的累计值

        #region 取得累计值里面的最小值

        /// <summary>
        /// 取得累计值里面的最小值
        /// </summary>
        /// <param name="sumList">累计值列表</param>
        /// <returns>最小值</returns>
        public static Decimal getMinValue(List<Decimal> sumList)
        {
            Decimal backDec = 0m;

            List<Decimal> tempLst = new List<decimal>();
            tempLst.AddRange(sumList);

            if (tempLst.Count > 0)
            {
                tempLst.Sort();
                backDec = tempLst[0];
                return backDec;
            }
            return backDec;
        }

        #endregion 取得累计值里面的最小值

        #region 读取指定日期指定股票的收盘价

        public static decimal getStockPriceByDate(string findDate, string filePath)
        {
            DateTime st = DateTime.ParseExact(findDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);

            List<StockDayInfo> LineList = new List<StockDayInfo>();
            try
            {
                #region 读取

                StreamReader objReader = new StreamReader(filePath);

                string sLine = "";
                StockDayInfo sdi = null;
                string oldSline = "";
                try
                {
                    while (sLine != null)
                    {
                        sLine = objReader.ReadLine();
                        if (sLine != null && !sLine.Equals(""))
                        {
                            try
                            {
                                string biginStr = sLine.Substring(0, 10);
                                DateTime splitDate = DateTime.ParseExact(biginStr, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                                if (st == splitDate)
                                {
                                    sdi = getStockInfoEntity(sLine);
                                    return sdi.EndPrice;
                                }
                                else if (splitDate > st)
                                {
                                    throw new Exception("不能找到股票价格");
                                    //break;
                                }
                                else
                                {
                                    oldSline = sLine;
                                }
                                //if (int.Parse(biginStr) < 2011) { continue; }
                            }
                            catch
                            {
                                continue;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    objReader.Close();
                }
                sdi = getStockInfoEntity(oldSline);
                return sdi.EndPrice;

                #endregion 读取
            }
            catch
            {
                throw new Exception("导出文件不存在，请重新导出");
            }

            //backDec = getEndPrice(st, LineList);
            //if (backDec != -1m)
            //{
            //    return backDec;
            //}
            //else
            //{
            //    throw new Exception("该日期的数据不存在，请检查导出的数据文件和日期是否正确");
            //}
        }

        private static decimal getEndPrice(DateTime st, List<StockDayInfo> listSdi)
        {
            StockDayInfo oldSdi = null;
            foreach (StockDayInfo newSdi in listSdi)
            {
                if (oldSdi == null)
                {
                    oldSdi = newSdi;
                }
                if (newSdi.StockDay == st)
                {
                    return newSdi.EndPrice;
                }
                else
                {
                    if (oldSdi.StockDay < st && newSdi.StockDay > st)
                    {
                        return oldSdi.EndPrice;
                    }
                    oldSdi = newSdi;
                }
            }

            return -1m;
        }

        private static StockDayInfo getStockInfoEntity(string strLine)
        {
            StockDayInfo stockDayInfo = new StockDayInfo();
            string[] aaa = strLine.Split(new string[] { "\t" }, StringSplitOptions.None);

            try
            {
                try
                {
                    stockDayInfo.StockDay = DateTime.ParseExact(aaa[0], "MM/dd/yyyy", CultureInfo.InvariantCulture);
                }
                catch
                {
                    stockDayInfo.StockDay = DateTime.ParseExact(aaa[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);
                }
                stockDayInfo.StartPrice = decimal.Parse(aaa[1]);
                stockDayInfo.MaxPrice = decimal.Parse(aaa[2]);
                stockDayInfo.MinPrice = decimal.Parse(aaa[3]);
                stockDayInfo.EndPrice = decimal.Parse(aaa[4]);
            }
            catch
            {
                return null;
            }
            return stockDayInfo;
        }

        #endregion 读取指定日期指定股票的收盘价
    }

    public class StockDayInfo
    {
        private DateTime stockDay;

        public DateTime StockDay
        {
            get
            {
                return stockDay;
            }
            set
            {
                if (stockDay == value)
                    return;
                stockDay = value;
            }
        }

        private decimal startPrice;

        public decimal StartPrice
        {
            get
            {
                return startPrice;
            }
            set
            {
                if (startPrice == value)
                    return;
                startPrice = value;
            }
        }

        private decimal maxPrice;

        public decimal MaxPrice
        {
            get
            {
                return maxPrice;
            }
            set
            {
                if (maxPrice == value)
                    return;
                maxPrice = value;
            }
        }

        private decimal minPrice;

        public decimal MinPrice
        {
            get
            {
                return minPrice;
            }
            set
            {
                if (minPrice == value)
                    return;
                minPrice = value;
            }
        }

        private decimal endPrice;

        public decimal EndPrice
        {
            get
            {
                return endPrice;
            }
            set
            {
                if (endPrice == value)
                    return;
                endPrice = value;
            }
        }
    }
}